package com.teufandsub.socialproximity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.google.android.gcm.GCMBaseIntentService;
import com.teufandsub.socialproximity.controller.Constants;
import com.teufandsub.socialproximity.controller.Controller;
import com.teufandsub.socialproximity.gui.ChattingActivity;
import com.teufandsub.socialproximity.gui.FriendListActivity;
import com.teufandsub.socialproximity.logic.Friend;
import com.teufandsub.socialproximity.logic.Message;
import com.teufandsub.socialproximity.logic.User;

public class GCMIntentService extends GCMBaseIntentService {

	@Override
	protected void onError(Context arg0, String arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onMessage(Context arg0, Intent arg1) {
		Controller controller = Controller.getInstance();
		
		if(arg1.getExtras().get(Constants.SERVICE_NAME_NAME).equals(Constants.MESSAGES_SERVICE)){
			
			Message message = new Message(arg1.getExtras().getString(Message.AUTHOR_NAME),
					arg1.getExtras().getString(Message.DEST_NAME), arg1.getExtras().getString(Message.CONTENT_NAME));
			
			Log.i(Constants.APP_NAME, "Message received");
			controller.receiveMessage(arg0,message);
			
		}else if(arg1.getExtras().get(Constants.SERVICE_NAME_NAME).equals(Constants.LIST_SERVICE)){
			String username = arg1.getExtras().getString(User.USERNAME_NAME);
			
			Intent intent = new Intent(arg0, FriendListActivity.class);
			PendingIntent pi = PendingIntent.getActivity(arg0, 0, intent, 0);
			
			Notification noti = new Notification(R.drawable.ic_launcher, arg0.getResources().getString(R.string.friendship_received) +" "+username
					, System.currentTimeMillis());
			noti.setLatestEventInfo(arg0, arg0.getResources().getString(R.string.friendship_received),
					username, pi);
			
			controller.notify(arg0, noti);
		}
	}

	@Override
	protected void onRegistered(Context arg0, String arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onUnregistered(Context arg0, String arg1) {
		// TODO Auto-generated method stub
		
	}

}
